package version

import (
	"context"
	"fmt"
	"github.com/Unixeno/islands/cmd/connection"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/gosuri/uitable"
	"github.com/spf13/cobra"
	"log"
)

func GetVersionCmd() *cobra.Command {
	return &cobra.Command{
		Use:   "version",
		Short: "version of Islands",
		Long:  `version of islands and islands api`,
		Args:  cobra.MinimumNArgs(0),
		Run:   handle,
	}
}

func handle(cmd *cobra.Command, args []string) {
	r, err := connection.Client.Version(context.Background(), &empty.Empty{})
	if err != nil {
		log.Fatalf("Cannot get Version info for reason: %v", err)
	}
	fmt.Println("Islands Client\n")
	fmt.Println("Server:")

	table := uitable.New()
	table.MaxColWidth = 100


	table.AddRow("  Api Version:", r.ApiVersion)
	table.AddRow("  Version:", r.Version)
	table.AddRow("  Go Version:", r.GoVersion)
	table.AddRow("  Built:", r.Built)
	table.AddRow("  Commit:", r.GitCommit)
	table.AddRow("  Arch:", r.Arch)
	fmt.Println(table)
}
